const {
  $util,
} = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    loadmore: true,
    directory: [],
    id: '',
    title: '党组织',
    showInfo: false,
    scrollId: `id0`,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id || '',
    });
    this.getList();
    this.getDirectory();
  },
  getList() {
    $util.request({
      url: 'entry/wxapp/partyman.organ.getorganstructure', // partyman.organ.getlist
      data: {
        id: this.data.id,
        page: this.data.page,
      },
      showLoading: true,
      success: (res) => {
        let list = {};
        list.members = res.members;
        list.organs = res.organs;
        list.mtotal = res.mtotal;
        if (this.data.page > 1) {
          list.members = [...this.data.members, ...res.members];
        }
        list.loadmore = res.members.length !== 0;
        list.page = res.members.length !== 0 ? this.data.page + 1 : this.data.page;
        this.setData(list);
      },
    });
  },
  // 获取子文件列表
  getChildList(e) {
    const {
      id,
      idx,
      title,
    } = e.currentTarget.dataset;
    let {
      directory,
      id: did,
    } = this.data;
    if (id === did) return;
    if (idx !== undefined) {
      directory.splice(idx, directory.length - idx);
    }
    this.setData({
      id: id,
      title: title,
      page: 1,
      keyword: '',
      directory,
    }, () => {
      this.getDirectory();
      this.getList();
    });
  },
  // 获取文件目录
  getDirectory() {
    let directory = this.data.directory;
    let item = [{
      id: this.data.id,
      title: this.data.title,
    }];
    directory = [...directory, ...item];
    this.setData({
      directory,
      scrollId: `id${this.data.id}`,
    });
  },
  // 获取党员信息
  getmemberInfo(e) {
    this.setData({
      showInfo: true,
      idx: e.currentTarget.dataset.idx, 
    });
  },
  hideInfo(e) {
    this.setData({
      showInfo: false,
    });
  },
  onReachBottom() {
    if (this.data.loadmore) this.getList();
  },
});